import { makeObservable, observable, flow, computed } from 'mobx';
import Apple from './Apple';

function queryData(number) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(new Apple(number));
    }, 1000);
  });
}

class Apples {
  constructor () {
    this.apples = [];
    this.picking = false;

    makeObservable(this, {
      apples: observable,
      picking: observable,
      pickApple: flow.bound,
      freshApples: computed,
      ateApples: computed,
    });
  }

  *pickApple () {
    this.picking = true;
    const data = yield queryData(this.apples.length + 1);
    this.apples.push(data);
    this.picking = false;
  }

  get freshApples () {
    return this.apples.filter(apple => !apple.ate);
  }

  get ateApples () {
    return this.apples.filter(apple => apple.ate);
  }
}

export default Apples;